cmake_minimum_required(VERSION 3.6)

project(mpc_controller)
find_package(carma_cmake_common REQUIRED)
carma_check_ros_version(2)

### Dependencies
find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()

# Generate code
acado_vendor_generate(
  GENERATED_PATH
  LIB_NAME single_track_mpc
  GENERATOR scripts/kinematic_bicycle_code_generator.cpp
  OUTPUT_PATH single_track_dynamics
  SNIPPET_PATHS "${motion_common_DIR}/../../autogeneration_code_snippets")

# Hack: Don't need libacado for general code; only need hpmpc if autogenerated code for that
list(REMOVE_ITEM ${PROJECT_NAME}_FOUND_BUILD_DEPENDS "acado_vendor")

# Build library
ament_auto_add_library(${PROJECT_NAME} SHARED
  src/mpc_controller/config.cpp
  src/mpc_controller/mpc_controller.cpp
  src/mpc_controller/references.cpp
  src/mpc_controller/debug.cpp
)
# TODO(#915) Linker error with -fvisibility=hidden
# autoware_set_compile_options(${PROJECT_NAME})
# target_compile_options(${PROJECT_NAME} PRIVATE -Wno-useless-cast -Wno-float-conversion -Wno-sign-conversion)
target_include_directories(${PROJECT_NAME} PRIVATE ${GENERATED_PATH})
target_link_libraries(${PROJECT_NAME} single_track_mpc)

### Test
if(BUILD_TESTING)
  # Linters
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
  # Unit test
  apex_test_tools_add_gtest(mpc_controller_unit_tests
    test/gtest_main.cpp
    test/sanity_checks.cpp)
  # TODO(#915) Linker error with -fvisibility=hidden
  # autoware_set_compile_options(mpc_controller_unit_tests)
  ament_target_dependencies(mpc_controller_unit_tests "controller_common")
  target_link_libraries(mpc_controller_unit_tests ${PROJECT_NAME} single_track_mpc)
  add_dependencies(mpc_controller_unit_tests ${PROJECT_NAME} single_track_mpc)
endif()

ament_auto_package()
